package com.example.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import java.util.List;

public class FilmsPage extends InternalPage {

    @FindBy(css = "a[href='./?go=add']")
    public WebElement addFilm;

    @FindBy(css = "div#results > a")
    public List<WebElement> linksToFilms;

    public MoviePage goToMovie(String movieName) {
        for (WebElement link: linksToFilms) {
            if (link.findElement(By.cssSelector("div.title")).getText().equals(movieName)) {
                if (link.isDisplayed()) {
                    link.click();
                    break;
                }
            }
        }

        return MyPageFactory.getPage(driver, MoviePage.class);
    }

    public boolean isOnThisPage() {
        return addFilm.isDisplayed();
    }

    @Override
    void tryToOpen() {
        MyPageFactory.getPage(driver, InternalPage.class).goToFilmsPage();
    }

}
